package com.hrt.manage.product.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hrt.bean.GiftDetail;
import com.hrt.bean.GiftGroup;
import com.hrt.manage.product.dao.GiftGroupDao;
import com.hrt.manage.product.service.GiftDetailService;
import com.hrt.manage.product.service.GiftGroupService;
import com.hrt.manage.product.service.ProDetailsService;
import com.hrt.vo.GiftGroupVO;

@Transactional
@Service("giftGroupService")
public class GiftGroupServiceImpl implements GiftGroupService{

	@Autowired
	private GiftGroupDao giftGroupDao;
	
	//产品规格
	@Autowired
	private ProDetailsService proDetailsService;
	
	//礼品规格
	@Autowired
	private GiftDetailService giftDetailService;
	
	/**
	 * 保存实体
	 * */
	@Override
	public void addGiftGroup(GiftGroup GiftGroup) {
		this.giftGroupDao.saveEntity(GiftGroup);
		
	}
	
	/**
	 * 更新实体
	 * */
	@Override
	public void updateGiftGroup(GiftGroup GiftGroup) {
		this.giftGroupDao.updateEntity(GiftGroup);
		
	}

	/**
	 * 删除实体
	 * */
	@Override
	public void delGiftGroup(Long id) {
		this.giftGroupDao.deleteEntity(id);
		
	}

	/**
	 * 按id查询实体
	 * */
	@Override
	public GiftGroup getById(Long id) {
		
		return this.giftGroupDao.getById(id);
	}
	
	/**
	 * 查询所有
	 * */
	@Override
	public List<GiftGroup> searchAll() {
		
		return this.giftGroupDao.findAll();
	}


	/**
	 * 按id列表查询实体
	 * */
	@Override
	public List<GiftGroup> getByIds(Long[] ids) {
		
		return this.giftGroupDao.getByIds(ids);
	}

	/**分页*/
	@Override
	public Long totalCount() {
		
		return this.giftGroupDao.totalCount();
	}
	@Override
	public List<GiftGroup> queryByPage(int pageIndex, int pageSize) {
		
		return this.giftGroupDao.queryByPage(pageIndex, pageSize);
	}
	
	/**父级下的分页*/
	@Override
	public List<GiftGroup> queryById(Long gdId, Integer pageIndex,
			Integer pageSize) {
		List<Map<String, Object>> conditions=new ArrayList<Map<String, Object>>();
		List<GiftGroup> list=new ArrayList<GiftGroup>();
		Map<String, Object> con=new HashMap<String, Object>();
		con.put("field", "giftDetail.id");
		con.put("value", gdId);
		
		Map<String, Object> delcon=new HashMap<String, Object>();
		delcon.put("field", "isdelete");
		delcon.put("value", 0);
		conditions.add(con);
		list=giftGroupDao.queryPageByCondition(conditions,pageIndex, pageSize);
		
		return list;
	}
	
	/**获取数据库的当前Date形式的时间*/
	@Override
	public Timestamp getDate() {
		
		return this.giftGroupDao.getDate();
	}

	/**删除选中的数据*/
	@Override
	public void deleteById(Long[] ids) throws Exception {
		List<GiftGroup> delResult = this.giftGroupDao.getByIds(ids);
		for (GiftGroup  gd: delResult) {
			gd.setIsdelete(new Integer(1));
			this.giftGroupDao.updateEntity(gd);
		}
	}
	/**
	 * 根据id删除订单详情
	 * 
	 */
	@Override
	public boolean delItemById(Long itemId) {
		GiftGroup item = this.giftGroupDao.getById(itemId);
		if (item != null) {
			item.setIsdelete(new Integer(1));
			this.giftGroupDao.updateEntity(item);
			return true;
		}
		return false;
	}
	
	/**
	 *将GiftGroupVO转换为GiftGroup  ---(修改);
	 */
	@Override
	public void updateGiftGroup(List<GiftGroupVO> gigrs) {
		try {
			for(GiftGroupVO gigr: gigrs) {
				GiftGroup giftGroup = new GiftGroup();
				if(gigr.getId()==null){
					BeanUtils.copyProperties(giftGroup, gigr);
					GiftDetail giftDetail = giftDetailService.getById(gigr.getGdeId());
					//礼品规格
					giftGroup.setGiftDetail(giftDetail);
					//礼品规格id
					giftGroup.setGdeId(gigr.getGdeId());
					//礼品编号
					giftGroup.setGiCode(giftDetail.getGiftCode());
					//礼品规格编号
					giftGroup.setGdeCode(giftDetail.getGdCode());
					
					// 根据产品规格id获取产品规格实体
					giftGroup.setProductDetails(proDetailsService.getById(gigr.getProId()));
					this.addGiftGroup(giftGroup);
				}else{
					BeanUtils.copyProperties(giftGroup, gigr);
					GiftDetail giftDetail = giftDetailService.getById(gigr.getGdeId());
					//礼品规格
					giftGroup.setGiftDetail(giftDetail);
					//礼品规格id
					giftGroup.setGdeId(gigr.getGdeId());
					//礼品编号
					giftGroup.setGiCode(giftDetail.getGiftCode());
					//礼品规格编号
					giftGroup.setGdeCode(giftDetail.getGdCode());
					// 根据产品规格id获取产品规格实体
					giftGroup.setProductDetails(proDetailsService.getById(gigr.getProId()));
					this.updateGiftGroup(giftGroup);
				}
			}
			
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
	}
	/**
	 *将GiftGroupVO转换为GiftGroup  ---(添加);
	 *//*
	@Override
	public List<GiftGroup> addGiftGroup(List<GiftGroupVO> gigrs) {
		List<GiftGroup> giftGroups = new ArrayList<GiftGroup>();
		try {
			for(GiftGroupVO gigr: gigrs) {
				GiftGroup giftGroup = new GiftGroup();
				BeanUtils.copyProperties(giftGroup, gigr);
				GiftDetail giftDetail = giftDetailService.getById(gigr.getGdeId());
				//礼品规格
				giftGroup.setGiftDetail(giftDetail);
				//礼品规格id
				giftGroup.setGdeId(gigr.getGdeId());
				//礼品编号
				giftGroup.setGiCode(giftDetail.getGiftCode());
				//礼品规格编号
				giftGroup.setGdeCode(giftDetail.getGdCode());
				
				// 根据产品规格id获取产品规格实体
				giftGroup.setProductDetails(proDetailsService.getById(gigr.getProId()));
				this.addGiftGroup(giftGroup);
				giftGroups.add(giftGroup);
			}
			
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
		return giftGroups;
	}
*/	
	/**根据礼品规格的ID找到产品组合的列表*/
	@Override
	public List<GiftGroup> getGiftGroupByGdId(Long gdId) {
		
		return this.giftGroupDao.queryGiftGroupByGdId(gdId);
	}

	@Override
	public void deleteGiftGroupByGdId(Long gdGrId) {
		GiftGroup gd = this.giftGroupDao.getById(gdGrId);
		gd.setIsdelete(new Integer(1));
		this.giftGroupDao.updateEntity(gd);
		
	}

	/**根据礼品规格的id找到礼品组合的id集合*/
	@Override
	public List<GiftGroup> getGroupIdforId(Long id) {
		return this.giftGroupDao.getGroupIdforId(id);
	}

	/**删除一个的数据*/
	@Override
	public void deleteByOneId(Long id) throws Exception {
		GiftGroup gigr = this.giftGroupDao.getById(id);
		gigr.setIsdelete(new Integer(1));
		this.giftGroupDao.updateEntity(gigr);
	}

	
	
}
